<?php
namespace app\common\validate;
use think\Validate;
use think\Request;
class Member extends Validate{
    protected $rule =   [
        'username'  => 'require|unique:member|max:20',
        'password' => 'require|min:6',
        'repassword'=>'require|confirm:password',
        'realname' => 'require',
        'phone'   => 'require|unique:member|number|length:11',
        'email' => 'require|email',
        'price' =>'require',
        'nickname' => 'require|max:30',
        'birthday' => 'require|checkBirthday',
        'gender' => 'require',
        'alipayno' => 'require',
        'totalprice' => 'require',
        'shouzhi' => 'require|checkShouzhi',
        'recom' => 'checkRecom',
        'paytype' => 'require|checkPaytype',
        'oldpassword' => 'require|checkOldPassword',
        'keywords' => 'require',
        'idcard' => 'require',
        'paypassword'=>'require',
        'cardno' => 'require',
        'cardtype' => 'require',
        'cardname' => 'require',
        'province' => 'require',
        'city' => 'require',
        'address' => 'require',
        'code' => 'require|checkCode',
    ]; 

    protected $message  =   [
        'username.require' => '用户名不为空',
        'username.max' => '用户名长度最多20位',
        'phone.require'   => '手机号不能为空',
        'phone.number'   => '手机号必须是数字',
        'phone.length'  => '手机号格式错误',
        'phone.unique'  => '手机号已被使用',
        'password.require' => '密码不能为空',
        'password.min' => '密码长度至少6位',
        'code' => '验证码不为空！',
        'repassword.require' => '确认密码不能为空',
        'repassword.confirm' => '两次输入密码不相同',
        'oldpassword.require' => '旧密码不能为空',
        'nickname.require' => '昵称不能为空',
        'nickname.max' => '昵称长度最多20位',
        'birthday.require' => '身份证格式不正确',
        'email' =>  '邮箱格式错误',
        'gender.require' => '性别不能为空',
        'alipayno.require' => '支付宝账号不能为空',
        'totalprice.require' => '金额不能为空',
        'shouzhi.require' => '转出银两不能为空',
        'qqkey.require' => 'openId不能为空',
        'avatar.require' => '头像不能为空',
        'paytype' => '需要选择提现类型',
        'keywords.require' => '关键字不能为空',
        'realname.require' => '姓名不能为空',
        'idcard.require' => '证件号码不能为空',
        'paypassword.require'=>'支付密码不能为空',
        'price.require'=>'价格不能为空',
        'cardno.require' => '银行卡号不能为空',
        'cardtype.require' => '银行卡类型不能为空',
        'cardname.require' => '开户人不能为空',
        'province.require' => '银行卡开户行地址不完整',
        'city.require' => '银行卡开户行地址不完整',
        'address.require' => '银行卡开户行地址不完整',
    ];

    protected $scene = [
        'add' => ['username', 'password', 'repassword', 'realname', 'phone', 'email'=>'email'],
        'edit' => ['realname', 'phone'=>'require|number|length:11|checkOldPhone', 'email'=>'email'],
        'edit_pwd' => ['realname', 'phone'=>'require|number|length:11|checkOldPhone', 'password', 'repassword', 'email'=>'email'],
        'login'  =>  ['phone'=>'require|checkStatus','password'],
        'other_login' => ['nickname', 'qqkey'=>'require', 'avatar'=>'require'],
        'bind_phone' => ['phone', 'code'],
        'bind_alipayno' => ['alipayno', 'code'],
        'check_phone' => ['phone'=>'require|number|length:11'],
        'bind_phone_check' => ['phone'],
        'check_phone_unique' => ['phone' => 'require|number|length:11|checkPhone'],
        'zhuanchu' => ['code', 'shouzhi'],
        'forget_pwd' => ['phone'=>'require|number|length:11|checkPhone', 'code', 'password','repassword'],
        'check_paytype' => ['paytype'=>'require', 'cardno', 'cardtype', 'cardname', 'province', 'city', 'address', 'code'],
        'check_keywords' => ['keywords'],
        'check_bir' => ['birthday'],
        'edit_paypwd' => ['phone'=>'require|number|length:11', 'code','paypassword'],
        'zhifu_pwd' => ['phone'=>'require|number|length:11', 'code','paypassword'],
        'check_paypassword' => ['paypassword'=>'require|checkPaypassword'],
    ];
    public function checkOldPhone($value, $rule=[], $data){
        $phone = model("Member")->where('id', $data['id'])->value('phone');
        if ($phone != $data['phone']) {
            $count = model('Member')->where('phone', $data['phone'])->count();
            if ($count) {
                return '手机号已被使用';
            }
        }
        return true;
    }
    public function checkStatus($value, $rule=[], $data){
        $status = model("Member")->where('username|phone|email', $data['phone'])->value('status');
        if ($status) {
            return '已经禁用，登录无效';
        }
        return true;
    }
    protected function checkBirthday($value, $rule=[], $data){
        list($year,$month,$day) = explode("-",$value);
        $year = date("Y") - $year;
        $month = date("m") - $month;
        $day  = date("d") - $day;
        if ($day < 0 || $month < 0){
            $year--;
        }
        if ($year < 16 || $year > 60) {
            return '年龄不符合';
        }
        return true;
    }
    protected function checkOldPassword($value, $rule=[], $data){
        $password = model("Member")->where('phone', $data['phone'])->value('password');
        if ($password !== md5($value)) {
            return '旧密码不正确';
        }else{
            return true;
        }
    }
    protected function checkPaytype($value, $rule=[], $data){
        if ($value == 1) {
            $alipayno = model("Member")->where('id', $data['userid'])->value('alipayno');
            if (!$alipayno) {
                return '你还没有绑定支付宝';
            }else{
                return true;
            }
        }elseif ($value == 2) {
            $info = model("MemberCard")->where('userid', $data['userid'])->find();
            if (empty($info)) {
                return '你还没有绑定银行卡';
            }else{
                return true;
            }
        }
        
    }
    protected function checkPhone($value, $rule=[], $data){
        $count = model("Member")->where('phone', $value)->count();
        if ($count) {
            return true;
        }else{
            return '手机号不存在';
        }
    }
    protected function checkShouzhi($value, $rule=[], $data){
        $tale = model("Member")->where('id', $data['userid'])->value('tale');
        if ($tale < $value) {
            return '银两不足';
        }else{
            return true;
        }
    }
    protected function checkCode($value, $rule=[],$data){
        $status = check_sms($data['phone'], $data['code']);
        if (!$status){
            return '验证码输入错误';
        }else{
            return true;
        }
    }
    protected function checkPassword($value, $rule=[], $data){
        $password = model("Member")->where('id', $data['userid'])->value('password');
        $value = md5($value);
        if ($value != $password) {
            return '密码错误';
        }else{
            return true;
        }
    }
    protected function checkTotalprice($value, $rule=[], $data){
        // if ($value < 100) {
        //     return '金额最少为100';
        // }
        // $amount = model("Member")->where('id', $data['userid'])->value('amount');
        // if ($value > $amount) {
        //     return '账号余额不足';
        // }
        return true;
    }
    protected function checkRecom($value, $rule=[], $data){
        $count = model("Member")->where('recom', $value)->count();
        if ($count) {
            return true;
        }else{
            return '验证码不存在';
        }
    }
    protected function checkPaypassword($value, $rule=[], $data){
        $uinfo = db("Member")->where('id', $data['userid'])->find();
        if (md5($value) != $uinfo['paypassword']) {
            return '支付密码错误';
        }
        return true;
    }
}